package com.tensquare.user.service;

import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.xml.ws.Action;

@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder encoder;
    //1. 添加管理员
    public boolean add(Admin admin) {
        //1.1 添加管理员id
        admin.setId(idWorker.nextId() + "");
        //1.2 加密管理员密码
        admin.setPassword(encoder.encode(admin.getPassword()));
        //1.3 添加管理员
        adminDao.save(admin);
        return true;

    }
    //2. 管理员登陆
    public Admin login(Admin admin) {
        //2.1 根据用户名查询用户是否存在
        Admin loginAdmin = adminDao.findByLoginname(admin.getLoginname());
        //2.2 用户存在，就判断密码是否相等
        if (loginAdmin != null && encoder.matches(admin.getPassword(),loginAdmin.getPassword())){
            return loginAdmin;
        }
        return null;
    }
    //3. 删除管理员
    public void dele(String adminId) {
        adminDao.deleteById(adminId);
    }
}
